package com.yanqu.road.dao.impl.activity.recoveryReward;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.retrievereward.RecoveryRewardUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class RecoveryRewardUserDataDaoImpl extends TempDataObjectDao<RecoveryRewardUserData> {
    public static String getRecoveryRewardUserDataSql = "select * from `t_u_recovery_reward` where `userId`=?";
    public RecoveryRewardUserData getRecoveryRewardUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getRecoveryRewardUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateRecoveryRewardUserDataSql = "update `t_u_recovery_reward` set lastRecoveryTime=? where userId=?";
    @Override
    protected boolean updateData(RecoveryRewardUserData recoveryRewardUserData) {
        recoveryRewardUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, recoveryRewardUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, recoveryRewardUserData.getUserId()));
        if (getDb().executeNoneQuery(updateRecoveryRewardUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertRecoveryRewardUserDataSql = "insert into `t_u_recovery_reward`(userId,lastRecoveryTime) values (?,?)";
    @Override
    protected boolean addData(RecoveryRewardUserData recoveryRewardUserData) {
        recoveryRewardUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, recoveryRewardUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, recoveryRewardUserData.getLastRecoveryTime()));
        if (getDb().executeNoneQuery(insertRecoveryRewardUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected RecoveryRewardUserData parseData(ResultSet rs) throws SQLException {
        RecoveryRewardUserData recoveryRewardUserData = new RecoveryRewardUserData();
        recoveryRewardUserData.setUserId(rs.getLong("userId"));
        recoveryRewardUserData.setLastRecoveryTime(rs.getLong("lastRecoveryTime"));

        return recoveryRewardUserData;
    }

}
